package multiThreading;

public class UnsynchronizedDemo
{
  public static void main(String args[])
  {
    SharedObject shared = new SharedObject();
    Caller ob1 = new Caller(shared, "Hello");
    Caller ob2 = new Caller(shared, "Synchronized");
    Caller ob3 = new Caller(shared, "World");

    // wait for threads to end
    try
    {
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    }
    catch (InterruptedException e)
    {
      System.out.println("Interrupted");
    }
  }

  static class Caller implements Runnable
  {
    String msg;

    SharedObject target;

    Thread t;

    public Caller(SharedObject targ, String s)
    {
      target = targ;
      msg = s;
      t = new Thread(this);
      t.start();
    }

    public void run()
    {
      target.call(msg);
    }
  }

  static class SharedObject
  {
    void call(String msg)
    {
      System.out.print("[" + msg);
      try
      {
        Thread.sleep(1000);
      }
      catch (InterruptedException e)
      {
        System.out.println("Interrupted");
      }
      System.out.println("]");
    }
  }
}
